﻿param
(
	[string]$SrcFileName = $( throw 'You must specify a source file name!' ),
	[string]$OutFileName = $( throw 'You must specify an output file name!' ),
	[string]$VersionMajor = $( throw 'You must specify major version!' ),
	[string]$VersionMinor = $( throw 'You must specify minor version!' )
)

$ErrorActionPreference = "Stop"

function IsSvnWC()
{
	return [System.IO.Directory]::Exists('.svn')
}

function IsTSvnInstalled()
{
	if ( Get-ChildItem Registry::HKLM\Software -Name | ? { $_ -eq 'TortoiseSVN' } )
	{
		return $true
	}

	return $false
}

function GetWCRevision()
{
	$revision = 1

	if (IsSvnWC -and IsTSvnInstalled)
	{
		$path = [System.IO.Path]::GetFullPath('.')
		$subwcrev = New-Object -ComObject SubWCRev.object
		$subwcrev.GetWCInfo($path, $true, $false)
		$revision = $subwcrev.Revision
	}

	return $revision
}

$revision = '0'
$build = (GetWCRevision).ToString()

if ($build.Length -gt 4)
{
	$revision = $build.Substring(0, $build.Length - 4)
	$build = $build.Substring($build.Length - 4)
}

$text = [System.IO.File]::ReadAllText($SrcFileName)

$text = $text.Replace('$(VersionMajor)', $VersionMajor)
$text = $text.Replace('$(VersionMinor)', $VersionMinor)
$text = $text.Replace('$(VersionRevision)', $revision)
$text = $text.Replace('$(VersionBuild)', $build)

[System.IO.File]::WriteAllText($OutFileName, $text, [System.Text.Encoding]::Unicode)
